package com.shop.seller.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.shop.domain.CustomSeller;
import com.shop.mapper.SellerMapper;
import com.shop.model.Seller;
import com.shop.model.SellerExample;
import com.shop.model.SellerExample.Criteria;
import com.shop.seller.service.SellerService;
/**
 * 
 * @author Y
 *
 */
@Service("sellerService")
public class SellerServiceBean implements SellerService {

	@Resource
	private SellerMapper sellerMapper;

	
	@Override
	public CustomSeller login(String username, String password) throws Exception {
		// TODO Auto-generated method stub
		SellerExample sellerExample = new SellerExample();
		Criteria criteria = sellerExample.createCriteria();
		criteria.andSellerIdEqualTo(username);//商家id
		criteria.andPasswordEqualTo(password);//密码
		//根据商家id和密码查询该用户是否存在
		List<Seller> sellers = this.sellerMapper.selectByExample(sellerExample);
		CustomSeller customSeller = null;
		//id(用户id是主键所以唯一)
		if(sellers.size()!=0){
			Seller seller = sellers.get(0);
			customSeller = new CustomSeller();
			BeanUtils.copyProperties(seller, customSeller);
		}
		
		return customSeller;
	}

	@Override
	public boolean register(CustomSeller seller) throws Exception {
		seller.setStatus("0");//标注商家的申请状态，0代表位审核
		seller.setCreateTime(new Date());//标注商家的创建时间
		int registerResult=this.sellerMapper.insert(seller);//进行商家注册
		if(registerResult>0){
			return true;//注册成功
		}else{
			return false;//注册失败
		}
	}

	@Override
	public boolean updateSellerMessage(CustomSeller seller) throws Exception {
		// TODO Auto-generated method stub
		int count = this.sellerMapper.updateByPrimaryKeySelective(seller);
		if(count>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateSellerPassword(String username,String password) throws Exception {
		Seller seller = new Seller();
		seller.setSellerId(username);
		seller.setPassword(password);
		int count = this.sellerMapper.updateByPrimaryKeySelective(seller);
		if(count>0){
			return true;
		}
		return false;
	}

	@Override
	public CustomSeller findSellerMessage(String id) throws Exception {
		// TODO Auto-generated method stub
		if(id!=null){
		Seller seller=this.sellerMapper.selectByPrimaryKey(id);
		CustomSeller customSeller = new CustomSeller();
		if(seller!=null){
			BeanUtils.copyProperties(seller, customSeller);
			return customSeller;
		}
	
		}
		return null;
	}
	
	@Override
	public List<String> findSellerNames() throws Exception {
		// TODO Auto-generated method stub
		SellerExample sellerExample = new SellerExample();
		List<Seller> lists=sellerMapper.selectByExample(sellerExample);
		List<String> sellerNames = new ArrayList<String>();
		for(Seller seller : lists){
			sellerNames.add(seller.getName());
		}
		return sellerNames;
	}
	
	@Override
	public boolean selectSeller(CustomSeller seller) throws Exception {
		SellerExample example=new SellerExample();
		Criteria criteria=example.createCriteria();
		String sellerId=seller.getSellerId();//用户名
		String nickName=seller.getNickName();//店铺名
		String licenseNumber=seller.getLicenseNumber();//营业执照号
		String taNumber=seller.getTaxNumber();//税务登记证号
		if(sellerId!=null){//用户名不为空
			criteria.andSellerIdEqualTo(sellerId);
		}
		if(nickName!=null){//店铺名不为空
			criteria.andNickNameEqualTo(nickName);
		}
		if(licenseNumber!=null){//营业执照号不为空
			criteria.andLicenseNumberEqualTo(licenseNumber);
		}
		if(taNumber!=null){//税务登记证号不为空
			criteria.andTaxNumberEqualTo(taNumber);
		}
		List<Seller> seller_DB=this.sellerMapper.selectByExample(example);
		if(!seller_DB.isEmpty()){//在数据库中查询到数据
			return true;
		}else{//在数据库中查询不到数据
			return false;
		}
	}
	

}
